package dao;

import bean.UsersInfo;
import db.DBManager;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class UsersInfoDAO {
    private Connection conn;
    private PreparedStatement ps;
    private ResultSet rs;

    public UsersInfo findUsers(UsersInfo ui){
        String sql = "select * from users where username=? and password=md5(?)";
        conn = DBManager.getConnection();
        try {
            ps = conn.prepareStatement(sql);
            ps.setString(1,ui.getUserName());
            ps.setString(2,ui.getPassWord());
            rs = ps.executeQuery();
            if (rs.next()){
                ui.setUserId(rs.getInt(1));
                ui.setUserSex(rs.getString(3));
                ui.setUserPhone(rs.getString(4));
                ui.setUserAddress(rs.getString(5));
                ui.setUserAge(rs.getInt(6));
            }else {
                ui = null;
            }
        }catch (SQLException e){
            System.out.println(e.getMessage());
        }
        return ui;
    }
}
